/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.commands.lane;

import java.util.UUID;

import org.fireflow.pdl.fpdl20.diagram.LaneShape;
import org.fireflow.pdl.fpdl20.diagram.PoolShape;
import org.fireflow.pdl.fpdl20.diagram.basic.Label;
import org.fireflow.pdl.fpdl20.diagram.basic.Rectangle;
import org.fireflow.pdl.fpdl20.diagram.basic.impl.LabelImpl;
import org.fireflow.pdl.fpdl20.diagram.impl.LaneShapeImpl;
import org.fireflow.pdl.fpdl20.diagram.style.BoundsStyle;
import org.fireflow.pdl.fpdl20.process.SubProcess;
import org.fireflow2.designer.eclipse.commands.AbstractCreateNodeCommand;
import org.fireflow2.designer.eclipse.figures.widget.PoolLaneBorder;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.LaneWrapper;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class CreateLaneCommand extends AbstractCreateNodeCommand {
	int index = 0;
	org.eclipse.draw2d.geometry.Rectangle clientArea = null;
	
	public void setIndex(int i){
		index = i;
	}
	public void setClientArea(org.eclipse.draw2d.geometry.Rectangle clientArea){
		this.clientArea = clientArea;
	}

	
	/* (non-Javadoc)
	 * @see org.fireflow2.designer.eclipse.commands.AbstractCreateNodeCommand#createNodeWrapper()
	 */
	@Override
	protected NodeWrapper createNodeWrapper() {
		NodeWrapper tmpNodeWrapper = null;
		String laneId = UUID.randomUUID().toString();
		
		LaneShape laneShape = new LaneShapeImpl(laneId);
		org.fireflow.pdl.fpdl20.diagram.basic.Rectangle fireRect =
				(org.fireflow.pdl.fpdl20.diagram.basic.Rectangle )laneShape.getShape();
		
		PoolShape poolShape = (PoolShape)this.parentWrapper.getDiagramElement();
		org.fireflow.pdl.fpdl20.diagram.basic.Rectangle poolRect = (org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)poolShape.getShape();
		
		int laneCount = poolShape.getLanes().size()>0?poolShape.getLanes().size()+1:1;
		
		fireRect.getBounds().getUpperLeftCorner().setX(0);//在FlowLayout布局中坐标无意义，故设置为0
		fireRect.getBounds().getUpperLeftCorner().setY(0);//在FlowLayout布局中坐标无意义，故设置为0
		fireRect.getBounds().setWidth(clientArea.width);
		//fireRect.getBounds().setHeight(clientArea.height/laneCount);//Y轴上取平均值
		fireRect.getBounds().setHeight(clientArea.height/2);
		
		if (fireRect.getTitle()==null){
			Label lb = new LabelImpl();
			fireRect.setTitleLabel(lb);
		}
		fireRect.getTitleLabel().setText(findANewNodeName(null,null));
		fireRect.getTitleLabel().getFont().setSize(10);
		fireRect.setContentLabel(null);//不需要contentlabel
		
		fireRect.getBounds().getBoundsStyle().setColor("#696969");
		
		fireRect.getBounds().getBoundsStyle().setLineType(BoundsStyle.LINETYPE_SOLID);
		fireRect.getBounds().getBoundsStyle().setThick(1);
		
		if (this.parentWrapper instanceof SubProcessWrapper){
			tmpNodeWrapper = new LaneWrapper((SubProcessWrapper)this.parentWrapper,laneShape);
		}else{
			tmpNodeWrapper = new LaneWrapper(((NodeWrapper)this.parentWrapper).getSubProcessWrapper(),laneShape);
		}
		
		return tmpNodeWrapper;
	}

	/* (non-Javadoc)
	 * @see org.fireflow2.designer.eclipse.commands.AbstractCreateNodeCommand#findANewNodeName(org.fireflow.pdl.fpdl20.process.Subflow, java.lang.String)
	 */
	@Override
	protected String findANewNodeName(SubProcess subflow, String nodeType) {
		PoolShape poolShape = (PoolShape)this.parentWrapper.getDiagramElement();
		if (poolShape.getLanes()==null || poolShape.getLanes().size()==0) return "Lane-1";
		return "Lane-"+(poolShape.getLanes().size()+1);
	}

	public void execute(){
		this.nodeWrapper = createNodeWrapper();
		if (nodeWrapper==null)return;
		parentWrapper.addChild(nodeWrapper,index);

	}
	
	public void redo(){
		if (nodeWrapper==null)return;
		parentWrapper.addChild(nodeWrapper,index);
	}
}
